package com.select;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import com.bean.users.usersBean;
import com.google.gson.Gson;
import com.util.usersUtil;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/usersServlet")
public class usersServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String type = req.getParameter("type");
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset:utf-8");
        resp.setCharacterEncoding("utf-8");
        if("1".equals(type)){
            try {
                List<usersBean> list = usersUtil.selectAll();
                resp.getWriter().write(new Gson().toJson(list));
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if("2".equals(type)){
            String amount = req.getParameter("amount");//业务员id
            String name = req.getParameter("name");//业务员账号
            String admin = req.getParameter("admin");//密码
            String type2 = req.getParameter("type2");//业务员身份
            try {
                usersUtil.update(name,admin,type2,amount);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if("3".equals(type)){
            String uid = req.getParameter("uid");
            try {
                usersUtil.delete(uid);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if("4".equals(type)){
            String userName = req.getParameter("name");
            String password = req.getParameter("admin");
            String status = req.getParameter("status");
            try {
                usersUtil.insert(userName,password,status);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
